package day14_io.demo01_文件和目录的copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

//复制一个目录下所有对象到另一个目录
public class Demo02copy {
    public static void main(String[] args) throws Exception{
        File file = new File("E:\\csw\\Demo\\csw1");
        File newfile = new File("E:\\csw\\Demo\\b");
        show(file,newfile);
    }
    public static void show(File file,File newfile) throws Exception{
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()){
                String s=newfile.getAbsolutePath()+"\\"+file1.getName();
                FileInputStream fis = new FileInputStream(file1.getAbsolutePath());
                FileOutputStream fos = new FileOutputStream(s);
                int i;
                byte[] b = new byte[1024];
                while ((i=fis.read(b))!=-1){
                    fos.write(b,0,i);
                }
                fis.close();
                fos.close();
            }else{
                File file2 = new File(newfile,file1.getName());
                file2.mkdirs();
                show(file1,file2);
            }
        }
    }
}
